package cn.zhixiangsingle.controller.cleanSoak;

import cn.zhixiangsingle.date.DateUtils;
import cn.zhixiangsingle.entity.base.IsEmptyUtils;
import cn.zhixiangsingle.entity.base.user.User;
import cn.zhixiangsingle.entity.cleanSoak.dto.CleanSoakDTO;
import cn.zhixiangsingle.entity.cleanSoak.vo.CleanSoakVO;
import cn.zhixiangsingle.excel.ExcelUtil;
import cn.zhixiangsingle.excel.ExcelWriterFactroy;
import cn.zhixiangsingle.service.cleanSoak.CleanSoakService;
import cn.zhixiangsingle.service.site.SiteService;
import cn.zhixiangsingle.web.responsive.IStatusMessage;
import cn.zhixiangsingle.web.responsive.ResultBean;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.metadata.Table;
import com.alibaba.excel.support.ExcelTypeEnum;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author hhp
 * @description 清洗类别维护view
 * @date 2019/11/13 13:41
 * @Copyright: 2019 www.zhixiangyun.net Inc. All rights reserved.
 * 注意：本内容仅限于浙江智飨科技内部传阅，禁止外泄以及用于其他的商业目
 */
@Controller
@RequestMapping("/cleanSoak")
public class CleanSoakController {
    private static final Logger logger = LoggerFactory
            .getLogger(CleanSoakController.class);
    @Reference(version = "1.0.0")
    private SiteService siteService;
    @Reference(version = "1.0.0")
    private CleanSoakService cleanSoakService;
    /**
     *@描述
     *@参数 []
     *@返回值 org.springframework.web.servlet.ModelAndView
     *@创建人 hhp
     *@创建时间 2019/11/13
     *@修改人和其它信息
     */
    @RequestMapping(value = "/getListPage", method = RequestMethod.GET)
    @ResponseBody
    public ModelAndView getListPage() {
        logger.debug("清洗类别维护信息列表！");
        ModelAndView mav = new ModelAndView("/qxjp/qxlbwh/qxlbwh");
        try {
            mav.addObject("msg", "ok");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("跳转清洗类别维护列表异常！", e);
        }
        return mav;
    }
    /**
     *@描述
     *@参数 [cleanSoakDTO]
     *@返回值 cn.zhixiangsingle.web.responsive.ResultBean
     *@创建人 hhp
     *@创建时间 2019/11/13
     *@修改人和其它信息
     */
    @PostMapping(value = "/getCleanSoakList")
    @ResponseBody
    public ResultBean getCleanSoakList(CleanSoakDTO cleanSoakDTO) {
        logger.debug("获取清洗类别维护列表！ cleanSoakDTO = "+cleanSoakDTO);
        ResultBean resultBean = new ResultBean();
        try {
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            cleanSoakDTO.setUserId(existUser.getId());
            resultBean = cleanSoakService.findCleanSoakList(cleanSoakDTO);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取清洗类别维护列表异常！", e);
            resultBean.setSuccess(false);
            resultBean.setResultCode(IStatusMessage.SystemStatus.ERROR.getCode());
            resultBean.setMsg(IStatusMessage.SystemStatus.ERROR.getMessage());
        }
        return resultBean;
    }
    /**
     *@描述
     *@参数 [response, cleanSoakDTO]
     *@返回值 void
     *@创建人 hhp
     *@创建时间 2019/11/13
     *@修改人和其它信息
     */
    @RequestMapping(value = "/writeCleanSoak", method = RequestMethod.GET)
    public void writeCleanSoaks(HttpServletResponse response, CleanSoakDTO cleanSoakDTO) {
        logger.debug("导出清洗类别维护信息Excel！条件:" + cleanSoakDTO);
        try {
            User existUser = (User) SecurityUtils.getSubject().getPrincipal();
            cleanSoakDTO.setUserId(existUser.getId());
            if (existUser.getZx() == true || existUser.getSdId() != 22) {
                cleanSoakDTO.setSdId(existUser.getSdId());
            }
            List<ResultBean> ingredientBasesModel = cleanSoakService.getCleanSoaks(cleanSoakDTO);
            ExcelWriterFactroy writer = new ExcelWriterFactroy(ExcelUtil.getOutputStream(DateUtils.formatDate(new Date(), "yyyy-MM-dd-HH-mm:ss-清洗类别维护信息"), response), ExcelTypeEnum.XLSX);
            CleanSoakVO ingredientBaseVO = new CleanSoakVO();
            if (!IsEmptyUtils.isEmpty(ingredientBasesModel)) {
                for (int i = 0; i < ingredientBasesModel.size(); i++) {
                    //sheetNo 不能从0开始
                    Sheet sheet = new Sheet(i + 1, 2, ingredientBaseVO.getClass());
                    sheet.setSheetName(ingredientBasesModel.get(i).getMsg());
                    sheet.setColumnWidthMap((Map) ingredientBasesModel.get(i).getObj());
                    Table table1 = new Table(1);
                    table1.setClazz(CleanSoakVO.class);
                    table1.setHead((List<List<String>>) ingredientBasesModel.get(i).getRows());
                    List<CleanSoakVO> ingredientBaseVOS = (List<CleanSoakVO>) ingredientBasesModel.get(i).getResult();
                    if (!IsEmptyUtils.isEmpty(ingredientBaseVOS)) {
                        writer.write(ingredientBaseVOS, sheet, table1);
                    }
                }
            }
            writer.finish();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("导出清洗类别维护Excel异常！", e);
        }
    }
}
